// pages/transfer/index.js
var util = require('../../utils/util.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    integral: "",
    userInfo: '',
    mobile: '',
    btntext: '获取验证码',
    code: '',
    isSend: false
  },

  setInputNum(e) {
    console.log(e.detail.value);
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      userInfo: util.isLogin()
    })
    util.http({
      url: 'api/miniwechat/center/integral',
      data: {
        user_id: this.data.userInfo.user_id
      }
    }).then((data) => {
      this.setData({
        transInfo: data,
        integral: {
          used: '',
          available: data.score,
          deduction: 0.00
        }
      })
      console.log(this.data.home)
    }).catch((err) => {
      wx.showToast({
        title: String(err),
        icon: 'none',
        duration: 2000
      })
    })
  },
  onIntegralChange(event) {
    let confirmIntegral = Number(event.detail.value > this.data.integral.available ? this.data.integral.available : event.detail.value),
      ded = confirmIntegral.toFixed(2);
    this.setData({
      ['integral.used']: confirmIntegral,
      ['integral.deduction']: ded
    })
  },
  getverificationCode: function () {
    if (this.data.isSend) {
      return;
    }
    var that = this;
    that.setData({
      isSend: true
    })

    let data = {
      mobile: this.data.userInfo.mobile,
      event: 'validate'
    }
    util.http({
      url: '/api/miniwechat/user/sendSms',
      method: 'POST',
      data
    }).then(res => {

      console.log(res);
      var restTimeStamp = new Date().getTime() + 60 * 1000;
      wx.setStorageSync('REST_COUNT_DOWN', restTimeStamp);
      countDown_60s(that, restTimeStamp);
    }).catch(ex => {
      wx.showToast({
        title: String(ex),
        icon: 'none',
        duration: 2000
      })
    })
  },
  phoneInput(e) {
    this.setData({
      mobile: e.detail.value
    })
  },
  codeInput(e) {
    this.setData({
      code: e.detail.value
    })
  },
  onSubmit() {
    let that = this;
    let phone = /^((13[0-9])|(14[5,7])|(15[0-3,5-9])|(17[0-8])|(18[0-9])|166|198|199|(147))\d{8}$/;
    if (this.data.mobile == this.data.userInfo.mobile) {
      wx.showToast({
        title: '不允许转让到自己账号',
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    if (this.data.integral.used === '' || this.data.integral.used == 0){
      wx.showToast({
        title: '输入积分有误',
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    if (this.data.mobile === '') {
      wx.showToast({
        title: '手机号码为空',
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    if (!(phone.test(this.data.mobile))) {
      wx.showToast({
        title: '手机号码输入有误',
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    if (this.data.code === '') {
      wx.showToast({
        title: '验证码输入有误',
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    util.http({
      url: 'api/miniwechat/center/integral',
      method: 'POST',
      data: {
        user_id: this.data.userInfo.user_id,
        mobile: this.data.userInfo.mobile,
        captcha: this.data.code,
        event: 'validate',
        score: this.data.integral.used,
        to_mobile: this.data.mobile
      }
    }).then((data) => {
      wx.showToast({
        title: '发送成功',
        icon: 'none',
        duration: 1000
      })
      setTimeout(() => {
        wx.navigateBack({
          delta: 1
        })
      }, 1000)
    }).catch((err) => {
      wx.showToast({
        title: String(err),
        icon: 'none',
        duration: 2000
      })
    })
  }
})
function countDown_60s(_this, restTimeStamp) {
  var nowtimeStamp = new Date().getTime();
  var coden = parseInt((restTimeStamp - nowtimeStamp) / 1000); // 定义60秒的倒计时
  if (coden > 0) {
    _this.setData({
      isSend: true
    })
    _this.data.codeV = setInterval(function () {
      _this.setData({
        btntext: (--coden) + 's'
      })
      if (coden < 0) { // 清除setInterval倒计时，这里可以做很多操作，按钮变回原样等
        clearInterval(_this.data.codeV);
        wx.removeStorageSync('REST_COUNT_DOWN');
        _this.setData({
          isSend: false,
          btntext: '重新发送'
        })
      }
    }, 1000) //  1000是1秒
  } else {
    _this.setData({
      isSend: false
    })
  }
}